package com.itbaizhan.service.impl;

import com.itbaizhan.domain.LoginUser;
import com.itbaizhan.service.LoginUserService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author spf
 * @date 2024/5/6
 * @time 15:10
 */
@Service
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private LoginUserService loginUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LoginUser loginUser = loginUserService.getLoginUser(username);
        if (loginUser == null){
            throw new RuntimeException("用户不存在!");
        }
        UserDetails userDetails = User.withUsername(username)
                .password(loginUser.getPassword())
                .authorities(new ArrayList<>())
                .build();
        return userDetails;
    }
}
